package monitTree;

import com.opensymphony.xwork2.ActionSupport;
import db_tool.DBConnection;
import db_tool.LogTool;
import db_tool.TimeFormat;
import org.apache.struts2.ServletActionContext;
import tree_util.UpLoadPicture;

import java.io.File;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * 添加、更新、删除	 沉淀物
 * */
public class OP_Sediment extends ActionSupport {

	private String flag;		//区分操作
	
	private final static String START = "SED";				//主键、照片 开头
	
	private static String PATH = "";					//照片在服务器上的路径("|"隔开)
	
	//private String SampleID;
	private String FatherText;
	private String ID_FractureSurface;
	public String getID_FractureSurface() {
		return ID_FractureSurface;
	}
	public void setID_FractureSurface(String iD_FractureSurface) {
		ID_FractureSurface = iD_FractureSurface;
	}

	String childID = START + TimeFormat.getNowTime();
	
	private List<File> Photo;					//文件引用
	private List<String> PhotoFileName;			//文件名字
	private List<String> PhotoContentType;		//文件类型
	private String isNull;
	
	private String deletePhoto;
	private String NodeName;
	
	private PrintWriter writer = null;
	
	private DBConnection db_connection = null;
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		writer = ServletActionContext.getResponse().getWriter();
		db_connection = DBConnection.getInstance();
		
		if(flag.equals("insert")){
			//插入操作
			insert();
			LogTool.insertIntoDataLog(LogTool.INSERT_CODE, "Sediment", 1);
		}else if(flag.equals("update")){
			//更新操作
			update();
			LogTool.insertIntoDataLog(LogTool.UPDATE_CODE, "Sediment", 1);
		}else if(flag.equals("query")){
			//查询沉积物是否已有
			allSediment();
		}else if(flag.equals("delete")){
			//删除照片
		   System.out.println("delete......................................"+NodeName+"     "+deletePhoto);
		   delete();
		   LogTool.insertIntoDataLog(LogTool.DELETE_CODE, "Sediment", 1);
		}if(flag.equals("getAllPhoto")){
			PATH = UpLoadPicture.getAllPhoto(NodeName);
			System.out.println("......................................"+NodeName+"             "+PATH);
			writer.write(PATH);
		}
		return NONE;
	}
	//查询沉积物是否已有
		private void allSediment(){
			String query = "select * from FractureSurface_Sediment where ID_FractureSurface='" + ID_FractureSurface + "'";
			
			int m = 0;
			Statement statement = null;
			ResultSet resultSet = null;
			try {
				statement = db_connection.getStatement();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				resultSet = statement.executeQuery(query);
				while(resultSet.next()){
					m ++;
				}
				if(m == 1){
					writer.write("enough");
					
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally{
				try {
					db_connection.close(resultSet, statement);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	    //删除照片操作
		private void delete(){
		    PATH = UpLoadPicture.Delete(NodeName,deletePhoto);
		    String update = "update Sediment set Photo='"
					+ PATH + "' where SampleID='" + NodeName + "'";
			System.out.println(update);
			Statement statement = null;
			try {
				statement = db_connection.getStatement();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				statement.executeUpdate(update);
				writer.write(PATH);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				writer.write("error");
				e.printStackTrace();
			} finally{
				try {
					db_connection.close(statement);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	
	private void update(){
		//获取照片路径
		if(isNull.length()!=0)
		PATH = UpLoadPicture.upload(Photo, PhotoFileName, FatherText);
		if(PATH.equals("photo_error")){			//照片出错
		writer.write("photo_error");
		}else{
				String sql = "";
				String sql1 = "";
				sql += "update Sediment set Photo='" + PATH + "'where SampleID='" + FatherText +  "'";
				//sql1 += "update FractureSurface_Sediment set ID_Sediment='" + SampleID + "' where ID_FractureSurface='" + FatherText + "'";
		
				System.out.println(sql);
				System.out.println(sql1);
				Statement statement = null;
		
				try {
					statement = db_connection.getStatement();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				try {
					statement.executeUpdate(sql);
					writer.write("success");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					writer.write("error");
					System.out.println("--沉淀物更新失败--");
					e.printStackTrace();
				} finally{
					try {
						db_connection.close(statement);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						System.out.println("沉淀物更新操作关闭失败");
						e.printStackTrace();
					}
				}
		}
		
	}
	private void insert(){
		//获取照片路径
		if(isNull.length()!=0){
		    PATH = UpLoadPicture.upload(Photo, PhotoFileName, childID);
		}
		if(PATH.equals("photo_error")){			//照片出错
			writer.write("photo_error");
		}else{
			String insert = "insert into Sediment values(?,?) ";
			String insert1 = "insert into FractureSurface_Sediment values(?,?)";
			
			PreparedStatement preparedStatement = null;
			
			//插入沉淀物表
			try {
				preparedStatement = db_connection.getPreparedStatement(insert);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				preparedStatement.setString(1, childID);
				preparedStatement.setString(2, PATH);
				
				preparedStatement.executeUpdate();
	
				System.out.println("插入成功!");
				writer.write(childID);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				writer.write("error");
				System.out.println("--沉淀物插入错误--");
				e.printStackTrace();
			}finally{
				try {
					db_connection.close(preparedStatement);
					System.out.println("插入成功!");
					//db_connection.close(preparedStatement1);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					System.out.println("沉淀物插入操作关闭失败");
					e.printStackTrace();
				}
			}
			
			//插入断面-沉淀物表
			try {
				preparedStatement = db_connection.getPreparedStatement(insert1);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				preparedStatement.setString(1, FatherText);
				preparedStatement.setString(2, childID);
				
				preparedStatement.executeUpdate();
				System.out.println(insert1);
				System.out.println("插入成功!");

				//writer.write(childID);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				writer.write("error");
				System.out.println("--断面-沉淀物插入错误--");
				e.printStackTrace();
			}finally{
				try {
					db_connection.close(preparedStatement);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					System.out.println("断面-沉淀物插入操作关闭失败");
					e.printStackTrace();
				}
			}
			
			
		}
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getFatherText() {
		return FatherText;
	}

	public void setFatherText(String fatherText) {
		FatherText = fatherText;
	}

	public List<File> getPhoto() {
		return Photo;
	}

	public void setPhoto(List<File> photo) {
		Photo = photo;
	}

	public List<String> getPhotoFileName() {
		return PhotoFileName;
	}

	public void setPhotoFileName(List<String> photoFileName) {
		PhotoFileName = photoFileName;
	}

	public List<String> getPhotoContentType() {
		return PhotoContentType;
	}

	public void setPhotoContentType(List<String> photoContentType) {
		PhotoContentType = photoContentType;
	}
	
	public String getDeletePhoto(){
		return deletePhoto;
	}
	
	public void setDeletePhoto(String deletePhoto){
		this.deletePhoto=deletePhoto;
	}
	
	public String getNodeName(){
		return NodeName;
	}
	
	public void setNodeName(String NodeName){
		this.NodeName=NodeName;
	}
	
	public String getIsNull(){
		return isNull;
	}
	
	public void setIsNull(String isNull){
		this.isNull=isNull;
	}


}
